package indi.zhifa.expshare.class001.chemicalmatch.exel;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import indi.zhifa.expshare.class001.chemicalmatch.dao.IChemicalCompositionRepository;
import indi.zhifa.expshare.class001.chemicalmatch.entity.dto.excel.ChemicalCompositionExcelData;
import indi.zhifa.expshare.class001.chemicalmatch.entity.po.ChemicalCompositionMongoData;
import indi.zhifa.engine.core.common.utils.DtoEntityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class ChemicalCompositionListener implements ReadListener<ChemicalCompositionExcelData> {

    List<ChemicalCompositionExcelData> mData;
    final MongoTemplate mMongoTemplate;
    final IChemicalCompositionRepository mChemicalCompositionRepository;

    public ChemicalCompositionListener(MongoTemplate pMongoTemplate,
                                       IChemicalCompositionRepository pChemicalCompositionRepository){
        mData = new ArrayList<>();
        mMongoTemplate = pMongoTemplate;
        mChemicalCompositionRepository = pChemicalCompositionRepository;
    }

    @Override
    public void invoke(ChemicalCompositionExcelData data, AnalysisContext context) {
        mData.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 首先，查询所有数据
        List<ChemicalCompositionMongoData> orgMongoData = mChemicalCompositionRepository.findAll();
        Map<String, ChemicalCompositionMongoData> mongoDataMap = new HashMap<>();
        for(ChemicalCompositionMongoData data : orgMongoData){
            mongoDataMap.put(data.key(), data);
        }

        List<ChemicalCompositionMongoData> newChemicalCompositionData = new ArrayList<>();
        List<ChemicalCompositionMongoData> updateChemicalCompositionData = new ArrayList<>();

        for(ChemicalCompositionExcelData data : mData){
            String paramType = "MISS";
            ChemicalCompositionMongoData theMongoData = DtoEntityUtil.trans(data, ChemicalCompositionMongoData.class);
            if(StringUtils.hasLength(data.getParam())){
                if(StrUtil.isNumeric(data.getParam())){
                    paramType = "NUM";
                    theMongoData.setNum(Integer.parseInt(data.getParam()));
                }else{
                    if("NR".equals(data.getParam())){
                        paramType = "NR";
                    }else{
                        paramType = "RANK";
                        theMongoData.setRank(data.getParam());
                    }
                }
            }
            theMongoData.setParamType(paramType);

            if(mongoDataMap.containsKey(theMongoData.key())){
                theMongoData.setId(mongoDataMap.get(theMongoData.key()).getId());
                updateChemicalCompositionData.add(theMongoData);
            }else{
                newChemicalCompositionData.add(theMongoData);
            }
        }
        if(!newChemicalCompositionData.isEmpty()){
            mMongoTemplate.insert(newChemicalCompositionData,"material");
        }
        if(!updateChemicalCompositionData.isEmpty()){
            BulkOperations ops = mMongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, ChemicalCompositionMongoData.class);
            for (ChemicalCompositionMongoData e : updateChemicalCompositionData) {
                // 根据 _id 定位文档，整个 entity 对象会被当作 replacement
                Query query = Query.query(Criteria.where("_id").is(e.getId()));
                ops.replaceOne(query, e);
            }
            ops.execute();
        }
    }
}
